/*
* @Author: zhangyujie
* @Date:   2015-08-12 10:44:47
* @Last Modified by:   zhangyujie
* @Last Modified time: 2015-08-13 13:12:12
*/

'use strict';

(function() {

	var Y = window.Y || {}

	var util = {

		getNumByRate: function(w, rate) {

			if (typeof rate === 'string' && rate.indexOf('%')) {

				return w * Number(rate.replace('%', '')) / 100
			} else if (typeof rate === 'string' && rate.indexOf('px')) {

				return Number(rate.replace('px', ''))
			} else {

				return Number(rate)
			}
		},
		getTextSize: function(text, style) {

			var span, position, width, height

			span = document.createElement('span')

			span.innerText = text

			for (var key in style) {

				span.style[key] = style[key]
			}

			document.body.appendChild(span)

			position = span.getBoundingClientRect()

			width = position.width

			height = position.height

			document.body.removeChild(span)

			return {

				width: width,
				height: height
			}
		}
	}

	Y.three_2 = function(setting) {

		var selector = setting.selector,
			series = setting.series,
			category = series.category,
			dataset = series.dataset,
			title = setting.title,
			text = title.text,
			subtext = title.subtext,
			textStyle = series.textStyle,
			titleTextStyle = title.textStyle,
			subtitleTextStyle = title.subtextStyle,
			totalSetting = setting.total,
			incomeSetting = setting.income,
			circleSetting = setting.circle,
			data = [],
			total = [],
			income = [],
			otherColor = setting.otherColor,
			colorset = setting.colorset,
			margin = setting.margin,
			width = document.querySelector(selector).offsetWidth,
			height = document.querySelector(selector).offsetHeight,
			length = dataset.length,
			rectHeight = height / length

		var svg,
			content,
			totalGroup,
			rectScale,
			incomeGroup,
			circle

		for(var i = 0,tempData;tempData = dataset[i++];){
			total.push(tempData['value'][0])
			income.push(tempData['value'][1])
		}
		data=[d3.sum(total),d3.sum(income).toFixed(2)]
		d3.select(title.selector).selectAll('div')
			.each(function(d,i){
				d3.select(this).select('dt').html(text[i]).style(titleTextStyle)
				d3.select(this).select('dd').html(data[i]).style(subtitleTextStyle)
			})

		rectScale = d3.scale.linear()
				.domain([0,1])
				.range([0,70])

		svg = d3.select(selector).append('svg')
				.attr('width',width)
				.attr('height',height)

		content = svg.append('g')
					.attr('class','.content')

		var categoryG = content.selectAll('.categoryG')
				.data(dataset)
				.enter()
				.append('g')
				.attr('class','categoryG')
				.attr('transform',function(d,i){
					return 'translate(' + 0 + ',' + i * rectHeight+ ')'
				})

		categoryG.append('rect')
				.attr('x',0)
				.attr('y',0)
				.attr('width',width)
				.attr('height',rectHeight)
				.attr('fill',function(d,i){
					return i % 2 == 0 ? otherColor[0] :otherColor[1]
				})
		content.select('.categoryG')
				.append('line')
				.attr('x1',0)
				.attr('y1',0)
				.attr('x2',width)
				.attr('y2',0)
				.attr('fill','none')
				.attr('stroke',otherColor[2])
				.attr('stroke-width','2')

		categoryG.append('line')
				.attr('x1',0)
				.attr('y1',function(d,i){
					return  rectHeight
				})
				.attr('x2',width)
				.attr('y2',function(d,i){
					return rectHeight
				})
				.attr('fill','none')
				.attr('stroke',otherColor[2])
				.attr('stroke-width','2')

		totalGroup = categoryG.append('g')
				.attr('class','totalGroup')
				.attr('transform',function(d,i){
					return 'translate(' + margin.left + ',' + margin.top+ ')'
				})

		totalGroup.append('circle')
				.attr('r',circleSetting['r'])
				.attr('cx',circleSetting['cx'])
				.attr('cy',circleSetting['cy'])
				.attr('fill',function(d,i){
					return colorset[i]
				})
		totalGroup.append('text')
				.text(function(d,i){
					return d['name']
				})
				.attr('x',totalSetting['x1'])
				.attr('y',totalSetting['y1'])
				.style(textStyle)
		totalGroup.append('text')
				.text(function(d,i){
					return category[0] + ' '
				})
				.attr('x',totalSetting['x2'])
				.attr('y',totalSetting['y2'])
				.style(textStyle)
		totalGroup.append('text')
				.text(function(d,i){
					return  d['value'][0] +'万'
				})
				.attr('x',totalSetting['x3'])
				.attr('y',totalSetting['y3'])
				.style(textStyle)
		incomeGroup = categoryG.append('g')
				.attr('class','incomeGroup')
				.attr('transform',function(d,i){
					return 'translate(' + margin.left + ',' + (margin.top + 40) + ')'
				})

		incomeGroup.append('text')
				.text(function(d,i){
					return '预期收益率 ' + (d['rate'] * 100).toFixed(1) + '%'
				})
				.attr('x',incomeSetting['x1'])
				.attr('y',incomeSetting['y1'])
				.style(textStyle)

		incomeGroup.append('rect')
				.attr('x',145)
				.attr('y',-11)
				.attr('width',function(d,i){
					return rectScale(d['rate'])
				})
				.attr('height',13)
				.attr('fill',function(d,i){
					return colorset[i]
				})

		incomeGroup.append('text')
				.text(function(d,i){
					return category[1] + ' '
				})
				.attr('x',incomeSetting['x2'])
				.attr('y',incomeSetting['y2'])
				.style(textStyle)
		incomeGroup.append('text')
				.text(function(d,i){
					return + d['value'][1] +'万'
				})
				.attr('x',incomeSetting['x3'])
				.attr('y',incomeSetting['y3'])
				.style(textStyle)

	}

	window.Y = Y

}())